<?php
namespace app\common\service;


class StoreRoleService extends BaseService
{

    /**
     * 后台: 获取角色列表
     * @param $sid
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getRoleList($sid)
    {
        $list = db('store_role')
                ->field('role_id,role_name,role_description,permissions,status')
                ->where(['sid' => $sid, 'status' => ['<>', -1]])->order('role_id DESC')
                ->select();
        if($list){
            foreach($list as $k => &$v){
                $v['permissions'] = $v['permissions'] ? explode(',', $v['permissions']) : 1;
            }
        }
        return $list;
    }

    /**
     * 后台: 新增或编辑角色
     * @param $sid
     * @param array $postParam
     * @return array
     * @throws \think\Exception
     */
    public function saveRole($sid, $postParam = [])
    {
        $data = [
            'role_name' => $postParam['role_name'],
            'role_description' => $postParam['role_description'],
            'permissions' => 1
        ];
        if ($postParam['role_id']) {
            $data['update_time'] = time();
            $r = db('store_role')->where(['sid' => $sid, 'role_id' => $postParam['role_id']])->update($data);
        } else {
            $info = db('store_role')->where(['sid' => $sid, 'role_name' => $postParam['role_name'], 'status' => 1])->find();
            if ($info) {
                return ['status' => 0, 'msg' => '角色名已存在'];
            }
            $data['sid'] = $sid;
            $data['add_time'] = time();
            $r = db('store_role')->insert($data);
        }

        return $r ? ['status' => 1, 'msg' => '编辑成功'] : ['status' => 0, 'msg' => '编辑失败'];
    }


    /**
     * 后台: 编辑角色状态
     * @param $sid
     * @param $role_id
     * @param $status
     * @return int
     */
    public function changeRoleStatus($sid, $role_id, $status)
    {
        $where = [
            'sid' => $sid,
            'role_id' => $role_id,
            'status' => ['<>', -1]
        ];

        // 删除
        if($status == -1){
            $info = db('store_admin')->where(['sid' => $sid, 'role_id' => $role_id, 'status' => 1])->find();
            if($info){
                return ['status' => 0, 'msg' => '该角色已被绑定，请解绑后再删除'];
            }
        }

        $r = db('store_role')->where($where)->setField('status', $status);
        return $r ? ['status' => 1, 'msg' => '修改成功'] : ['status' => 0, 'msg' => '修改失败'];
    }


    /**
     * 后台; 设置角色权限
     * @param $sid
     * @param $role_id
     * @param $permissions
     * @return int
     */
    public function setPermissions($sid, $role_id, $permissions)
    {
        $where = [
            'sid' => $sid,
            'role_id' => $role_id,
            'status' => ['<>', -1]
        ];

        $permissions = $permissions ? implode(',', $permissions) : '';
        return db('store_role')
                ->where($where)
                ->setField('permissions', $permissions);

    }


    /**
     * 后台: 获取角色权限信息
     * @param $sid
     * @param $role_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function getRoleDetail($sid, $role_id)
    {
        $where = [
            'sid' => $sid,
            'role_id' => $role_id,
            'status' => ['<>', -1]
        ];
        $info = db('store_role')->field('role_id,role_name,permissions')->where($where)->find();
        if(!$info){
            return false;
        }

        $info['permissions'] = explode(',', $info['permissions']);
        return $info;
    }
}